package com.qdu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseDelete {
    public static void main(String[] args) {
        // HBase的连接以及删除t_log表中指定rowkey的数据
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "niit-master");
        conf.setInt("hbase.rpc.timeout", 20000);
        conf.setInt("hbase.client.operation.timeout", 20000);
        conf.setInt("hbase.client.scanner.timeout.period", 20000);

        // 实现HBASE连接，表连接，数据删除等逻辑
        try (
                Connection conn = ConnectionFactory.createConnection(conf);
                Table tlog = conn.getTable(TableName.valueOf("t_log"))
        ) {

            // 创建Delete对象
            Delete delete = new Delete(Bytes.toBytes("X1"));
            // 添加要删除的当前行的某个列族或者列
            delete.addColumn(
                    Bytes.toBytes("f"),
                    Bytes.toBytes("myname")
            );
            // 调用表的delete方法，执行delete命令
            tlog.delete(delete);
            System.out.println("Data deleted successfully.");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
